package com.frogs.ucenter.v1.request;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.MapUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;

/**
 * 添加邮件系统收件人
 * @author hongyuhao
 *
 */
public class MailingRecieverAddRequest extends Request {

	//用户邮箱地址
	private String email;
	
	//用户从属的分组ID（需在邮件系统获得）
	private int groupId;
	
	//用户从属的活动ID（需在邮件系统获得）
	private int compId;
	
	//用户参数字段,根据邮件系统后台的设置项添加配置字段
	private Map<String, Object> userParam = new HashMap<String,Object>(); 
	
	//用户参数json串
	private String paramJson;
	
	@Override
	public void parse(HttpServletRequest request) {
		
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		
		email = request.getParameter(ApiUrlParamEnum.email.getParamName());
		if(StringUtil.isEmpty(email)) {
			throw new RequestIllegalException("缺少邮箱地址参数！");
		} else if(!StringUtil.isEmail(email)) {
			throw new RequestIllegalException("邮箱地址格式错误！");
		}
		
		groupId = requestInfo.getIntParameter(ApiUrlParamEnum.groupId.getParamName(), -1);
		if(groupId < 0) {
			throw new RequestIllegalException("缺少分组ID！");
		}
		compId = requestInfo.getIntParameter(ApiUrlParamEnum.campaignId.getParamName(), -1);
		if(compId < 0) {
			throw new RequestIllegalException("缺少活动ID！");
		}
		
		paramJson = requestInfo.getParameter(ApiUrlParamEnum.param.getParamName());
		if(!StringUtil.isEmpty(paramJson)) {
			try {
				userParam = MapUtil.transferFromObject(JSONObject.parseObject(paramJson));
			} catch (Exception e) {
				throw new RequestIllegalException("参数格式有误");
			}
		}
		
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getCompId() {
		return compId;
	}

	public void setCompId(int compId) {
		this.compId = compId;
	}

	public Map<String, Object> getUserParam() {
		return userParam;
	}

	public void setUserParam(Map<String, Object> userParam) {
		this.userParam = userParam;
	}
	
}
